/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cashmate.hibernate.dao;

import com.cashmate.hibernate.pojo.Borrowerinfo;
import com.cashmate.hibernate.pojo.Businesses;
import com.cashmate.hibernate.pojo.CollateralCreditinv;
import com.cashmate.hibernate.pojo.Creditinvestigations;
import com.cashmate.hibernate.pojo.Documents;
import com.cashmate.hibernate.pojo.Documentspassed;
import com.cashmate.hibernate.pojo.Informants;
import com.cashmate.hibernate.pojo.Notesreceivable;
import com.cashmate.hibernate.pojo.Othersourceofincome;
import com.cashmate.hibernate.pojo.Personalandrealproperties;
import com.cashmate.hibernate.pojo.ReputationCreditinv;
import com.cashmate.hibernate.pojo.SourceCreditinv;
import com.cashmate.hibernate.pojo.Vehicle;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;

/**
 *
 * @author Lee
 */
public class CreditinvestigationDAO extends DAO {

    public CreditinvestigationDAO() {
        super();
    }

    //BASIC DAO METHODS
    @SuppressWarnings("unchecked")
    public List<Creditinvestigations> getAll() throws Exception {
        List<Creditinvestigations> a = (List<Creditinvestigations>) getSession().createQuery("from Creditinvestigations a").setCacheable(true).list();
        flush();
        return a;
    }

    @SuppressWarnings("unchecked")
    public Creditinvestigations get(int id) throws Exception {
        Creditinvestigations a = (Creditinvestigations) getSession().createQuery("from Creditinvestigations a where a.creditinvid=:id").setParameter("id", id).setCacheable(true).uniqueResult();
        flush();
        return a;
    }

    @SuppressWarnings("unchecked")
    public void update(Creditinvestigations a) throws Exception {
        try {
            begin();
            getSession().update(a);
            commit();
            flush();
        } catch (Exception e) {
            rollback();
            throw e;
        }
    }

    @SuppressWarnings("unchecked")
    public void insert(Creditinvestigations a) throws Exception {
        try {
            begin();
            getSession().save(a);
            commit();
            flush();
        } catch (Exception e) {
            rollback();
            throw e;
        }
    }

    @SuppressWarnings("unchecked")
    public void delete(int id) throws Exception {
        try {
            begin();
            getSession().delete(get(id));
            commit();
            flush();
        } catch (Exception e) {
            rollback();
            throw e;
        }
    }

    @SuppressWarnings("unchecked")
    public List<Creditinvestigations> findByCriteria(DetachedCriteria dc, int from, int size) throws Exception {
        Criteria criteria = dc.getExecutableCriteria(getSession());
        criteria.setFirstResult(from);
        criteria.setMaxResults(size);
        criteria.setCacheable(true);
        List<Creditinvestigations> list = criteria.list();
        flush();
        return list;
    }

    @SuppressWarnings("unchecked")
    public int countByCriteria(DetachedCriteria dc) throws Exception {
        Criteria criteria = dc.getExecutableCriteria(getSession());
        criteria.setCacheable(true);
        criteria.setProjection(Projections.rowCount());
        int count = ((Integer) criteria.list().get(0)).intValue();
        flush();
        return count;
    }

    //OTHER METHODS
    @SuppressWarnings("unchecked")
    public boolean addCreditinvestigation(Documents d, Creditinvestigations c, Borrowerinfo b, List<Documentspassed> listdocs, List<Informants> listinf,
            List<Personalandrealproperties> listper, List<Othersourceofincome> listoth, List<Vehicle> listveh, List<Notesreceivable> listnotes,
            List<ReputationCreditinv> listrci, List<CollateralCreditinv> listcci, List<SourceCreditinv> listsci, List<Businesses> listbus) throws Exception {
        try {
            Session session = getSession();
            begin();
            session.save(d);
            //System.out.println(listdocs.size() + "documents were saved.");

            session.save(c);
            //System.out.println(listdocs.size() + "credits were saved.");

            session.save(b);
            //System.out.println(listdocs.size() + "borrowerinfo was saved.");

            for (int i = 0; i < listdocs.size(); i++) {
                session.save(listdocs.get(i));
            }
            //System.out.println(listdocs.size() + "documents passed were saved.");

            for (int i = 0; i < listinf.size(); i++) {
                //System.out.println(listinf.get(i).toString());
                session.save(listinf.get(i));
            }
            //System.out.println(listinf.size() + "informants were saved.");

            for (int i = 0; i < listper.size(); i++) {
                session.save(listper.get(i));
            }
            //System.out.println(listper.size() + "properties were saved.");

            for (int i = 0; i < listoth.size(); i++) {
                session.save(listoth.get(i));
            }
            //System.out.println(listoth.size() + "other sources were saved.");

            for (int i = 0; i < listveh.size(); i++) {
                session.save(listveh.get(i));
            }
            //System.out.println(listveh.size() + "vehicles were saved.");

            for (int i = 0; i < listnotes.size(); i++) {
                session.save(listnotes.get(i));
            }
            //System.out.println(listnotes.size() + "savings/other notes receivables were saved.");

            for (int i = 0; i < listrci.size(); i++) {
                session.save(listrci.get(i));
            }
            //System.out.println(listrci.size() + "reputations were saved.");

            for (int i = 0; i < listcci.size(); i++) {
                session.save(listcci.get(i));
            }
            //System.out.println(listcci.size() + "collaterals were saved.");

            for (int i = 0; i < listsci.size(); i++) {
                session.save(listsci.get(i));
            }
            //System.out.println(listsci.size() + "sources of income were saved.");

            for (int i = 0; i < listbus.size(); i++) {
                session.save(listbus.get(i));
            }
            //System.out.println(listbus.size() + "businesses were added.");

            commit();
            flush();
            return true;
        } catch (Exception ex) {
            rollback();
            throw ex;
        }
    }

    @SuppressWarnings("unchecked")
    public Creditinvestigations getByLoanAndDate(int loanid, String date) throws Exception{
        Creditinvestigations c = (Creditinvestigations) getSession().createQuery("from Creditinvestigations c where c.loanappid=:lid and c.investigationdate=:d ").setParameter("lid", loanid).setParameter("d", date).setMaxResults(1).setCacheable(true).uniqueResult();
        flush();
        return c;
    }

// Use get (int id) above... 
//    @SuppressWarnings("unchecked")
//    public Creditinvestigations getById(int cid) {
//        Creditinvestigations c = null;
//        try {
//            c = (Creditinvestigations) session.createQuery("from Creditinvestigations c where c.creditinvid=:cid").setParameter("cid", cid).setMaxResults(1).uniqueResult();
//        } catch (Exception ex) {
//            ex.printStackTrace();
//        }
//        return c;
//    }

    @SuppressWarnings("unchecked")
    public Creditinvestigations getLatestByLoanId(int loanappid) throws Exception{
        Creditinvestigations c = (Creditinvestigations) getSession().createQuery("from Creditinvestigations c where c.loanapplications.loanappid=:lid order by c.creditinvid desc").setParameter("lid", loanappid).setMaxResults(1).setCacheable(true).uniqueResult();
        flush();
        return c;
    }
    
    @SuppressWarnings("unchecked")
    public Creditinvestigations getLatestByLoanverId(int loanverid) throws Exception {
        Creditinvestigations c = (Creditinvestigations) getSession().createQuery("from Creditinvestigations c where c.loanverifications.loanverificationid=:lid order by c.creditinvid desc")
                    .setParameter("lid", loanverid).setMaxResults(1).uniqueResult();
        flush();
        return c;
    }
}
